﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using S.Services.SheepMale.Domain.PurchaseSales;
using S.Services.SheepMale.Domain.Select;
using S.Services.SheepMale.Infrastructure;
using S.Services.SheepMale.Read.Api.Application.ReadCommand.ReadMarketCommand;
using System.Diagnostics.Metrics;

namespace S.Services.SheepMale.Read.Api.Application.ReadCommandHandler.ReadMarketCommandHandler
{
	public class MarkCountCommandHandler : IRequestHandler<MarketCountCommand, CountInfo<EntityMarket>>
	{
		private readonly IRepository<EntityMarket> repository;

		private readonly CountInfo<EntityMarket> count;

		public MarkCountCommandHandler(IRepository<EntityMarket> repository, CountInfo<EntityMarket> count)
		{
			 this.repository = repository;
			 this.count = count;
		}

		public async Task<CountInfo<EntityMarket>> Handle(MarketCountCommand request, CancellationToken cancellationToken)
		{
			var list = await repository.AllAsync();
			var result = repository.AllAsync().Result.Where(x => x.Client == request.client&&x.DocumentNumber==request.number);
			if (result != null)
			{
				count.CountNumber = result.Count();
				count.totalPrice = result.Sum(x => x.PayPrice);
				count.datas = result.ToList();
				return  count;
			}
			count.datas = list.ToList();
			return count;
		}
	}
}
